/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openjawtech.xscraper.scraper;

import com.openjawtech.xscraper.scraper.runtime.xml.ScrapeXDB;
import com.openjawtech.xscraper.scraper.util.StringUtil;
import com.openjawtech.xscraper.scraper.util.FileUtil;
import com.openjawtech.xscraper.schema.scrape_config.ScrapeGroupsType;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @Created on 24-Apr-2009, 07:48:10
 * @author Wenbo.Sun
 */
public class ScrapeConfiguration {

    public static final String HTMLDIRSPROPERTY = "scrape.source.dirs.html";
    public static final String XMLDIRSPROPERTY = "scrape.source.dirs.xml";
    public static final String DELETEDIRSPROPERTY = "scrape.source.dirs.delete";
    private Properties configProperties;
    private static final Log log = LogFactory.getLog(ScrapeConfiguration.class);

    public ScrapeConfiguration(Properties configProperties) {
        this.configProperties = configProperties;
    }

    public ScrapeGroupsType loadConfigFile(String file) {
        ScrapeXDB sxdb = new ScrapeXDB();
        return sxdb.unMarshall(file);
    }

    public List<File> getHTMLDirs() {
        log.debug("get html resource dirs ...");
        return getDirs(HTMLDIRSPROPERTY);
    }

    public List<File> getXMLDirs() {
        log.debug("get xml resource dirs");
        return getDirs(XMLDIRSPROPERTY);
    }

    public List<File> getDeleteDirs() {
        log.debug("get delete resource dirs");
        return getDirs(DELETEDIRSPROPERTY);
    }

    public List<File> getDirs(String dirProperties) {
        List<File> list = new ArrayList<File>();
        try {
            String pro = configProperties.getProperty(dirProperties);
            if (pro == null || "".equals(pro))
                return list;
            String[] dirs = StringUtil.commonSeparate(pro);
            for (String dir : dirs) {
                File file = new File(dir);
                if (FileUtil.isFolderAndExisted(file)) {
                    list.add(file);
                }
            }
        } catch (RuntimeException re) {
            log.error("get dirs failed! ", re);
        }
        return list;
    }
}
